#include "CircleEmitter.h"
#include "math.h"
CCircleEmitter::CCircleEmitter(void): m_position(this, Vector3(0, 0, 0)), m_radius(this, 1){}

CCircleEmitter::~CCircleEmitter(void){}

void CCircleEmitter::emitterUpdate()
{
	//get the array of particles and the number of particle
	float* arrayOfParticleSystem=m_particleSystem->m_arrayOfParticles;
	int numberOfParticle=m_particleSystem->m_numberOfParticle;

	//for every particle to emit
	for(int i=0; i<m_numberOfParticleToEmit.getValue(); i++)
	{
		int position=0;
		const int numberOfElementPerParticle=8;

		//look for the index of an empty particle
		while(arrayOfParticleSystem[position+7]!=-1 && position<numberOfParticle*numberOfElementPerParticle)
		{
			position+=numberOfElementPerParticle;
		}

		//if we did'nt find an empty particle = exit
		if(position>=numberOfParticle*numberOfElementPerParticle)
			return;

		//set the position of the particle
		float positionInaccuracy=(-1+rand()%2)/10;
		arrayOfParticleSystem[position]=cos(float(i))*m_radius.getValue()+m_position.getValue().x+positionInaccuracy;
		arrayOfParticleSystem[position+2]=sin(float(i))*m_radius.getValue()+m_position.getValue().y+positionInaccuracy;
		arrayOfParticleSystem[position+1]=0;

		//set the speed
		float speedInaccuracy=(-5+rand()%10);
		arrayOfParticleSystem[position+3]=m_velocity.getValue().x*cos(float(i))*speedInaccuracy;
		arrayOfParticleSystem[position+4]=m_velocity.getValue().y*sin(float(i))*speedInaccuracy;
		arrayOfParticleSystem[position+5]=m_velocity.getValue().z;

		//set time and alive
		arrayOfParticleSystem[position+6]=m_particleSystem->m_time.getValue();
		arrayOfParticleSystem[position+7]=1;
	}

}
